package com.jinly.ryder.account.web.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpServletRequest;

@Slf4j
public class IpUtil {

    public static String[] headerNames = new String[]{"X-Forwarded-For", "Proxy-Client-IP", "WL-Proxy-Client-IP", "HTTP_CLIENT_IP", "HTTP_X_FORWARDED_FOR"};

    /**
     * 获取客户端ip
     *
     * @param request
     * @return
     */
    public static String getRemoteIp(HttpServletRequest request) {
        for (String headerName : headerNames) {
            String value = request.getHeader(headerName);
            if (!ObjectUtils.isEmpty(value) && !"unknown".equalsIgnoreCase(value)) {
                return value.split(",")[0];
            }
        }
        return request.getRemoteAddr();
    }
}
